<?php
/**
 * Created by PhpStorm.
 * User: 张福来
 * Date: 2018/10/27
 * Time: 14:43
 */
namespace app\api\model;
use think\Db;
use think\Model;
class Cash extends Model{
    protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();
        //TODO:自定义的初始化
    }
    protected function getCtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    protected function getUtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    /*
    * Notes: 添加商户
    * Create Date: 2018/10/27
    * Create Time: 15:39
    * Create by :张福来
    * Param :
    */
    public function addCash($addarray){
        $reslt = $this->save($addarray);
        if($reslt){
            return $this->a_id;
        }else{
            return false;
        }
    }
    public function editCash($addarray){
        $a_id = $addarray['a_id'];
        unset($addarray['a_id']);
        $reslt = $this
                 ->where(["a_id"=>$a_id])
                 ->update($addarray);
        return $reslt;
    }
    public function deleteCash($a_id){
        $reslt = $this
                 ->where(["a_id"=>$a_id])
                 ->update(["status"=>9]);
        return $reslt;
    }
    public function selectCash($pageNum,$listNum,$w){
        $reslt["list"] = $this
                 ->alias("c")
                 ->field("c.account,c.contactnum,c.a_id,c.province,c.city,c.area,c.lon,c.lat,c.last_login_time,c.last_login_ip,c.legal_person,c.id_number,
                 c.email,c.content,c.pic,c.name,c.wallet,c.detailed_address,c.ctime,c.status,c.utime,a.area_name province_name,v.area_name city_name,x.area_name area_name")
                 ->where($w)
                 ->join("__AREAS__ a","c.province = a.area_id")
                 ->join("__AREAS__ v","c.city = v.area_id")
                 ->join("__AREAS__ x","c.area = x.area_id")
//                 ->join("__ATTESTATION__ d","d.id IN d.attestation_id")
//                 ->where("d.id","IN","c.attestation_id")
                 ->page($pageNum,$listNum)
                 ->order("c.ctime desc")
                 ->select();

        $reslt["total"] = $this
                ->alias("c")
                ->where($w)
                ->count("a_id");
        return $reslt;

    }
    public function nearCash($pageNum,$listNum,$w,$lat,$lnt){
        $reslt["list"] = $this
            ->alias("c")
            ->field("c.a_id,c.pic,c.name,c.detailed_address,c.attestation_id,c.score,c.contactnum,c.lon,c.lat,a.area_name province_name,v.area_name city_name,x.area_name area_name,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lnt*PI()/180-lon*PI()/180)/2),2)))*1000) AS distance")
            ->where($w)
            ->join("__AREAS__ a","c.province = a.area_id")
            ->join("__AREAS__ v","c.city = v.area_id")
            ->join("__AREAS__ x","c.area = x.area_id")
//                 ->join("__ATTESTATION__ d","d.id IN d.attestation_id")
//                 ->where("d.id","IN","c.attestation_id")
            ->page($pageNum,$listNum)
            ->order("distance asc")
            ->select();
        $reslt["attestation"] = $this
            ->query("SELECT C.a_id, GROUP_CONCAT(A. `NAME`) att_name,GROUP_CONCAT(A. pic) pic_path FROM orange_cash C LEFT JOIN orange_attestation A ON FIND_IN_SET(A.id, C.attestation_id) GROUP BY C.a_id");
        $reslt["total"] = $this
            ->alias("c")
            ->where($w)
            ->count("a_id");
        return $reslt;
    }

    /**
     * Notes: 查找商户信息
     * Create Date: 2018/10/2
     * Create Time: 15:34
     * Create by :lijie
     */
    public function findCash($w){

        $result = $this->where($w)->find();
        return $result;
    }

    /**
     * Notes:  修改商户
     * Create Date: 2018/10/30
     * Create by :lijie
     */
    public function editCashAttest($param,$where)
    {
        unset($param['a_id']);
        return $this->isUpdate(true)->allowField(true)->save($param,$where);
    }

    /**
     * Notes:  商户评分
     * Create Date: 2018/10/22
     * Create by :lijie
     */
    public function scoreCash($where)
    {
        return $this
                ->field("score")
                ->where($where)
                ->find();
    }
}
